﻿package org.cwnuit.train.acton;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.train.domain.Trains;
import org.cwnuit.train.domain.Users;
import org.cwnuit.train.service.RulesService;
import org.cwnuit.train.service.SworksService;
import org.cwnuit.train.service.TrainsService;
import org.cwnuit.train.utils.CreateExcel;
import org.cwnuit.train.utils.PageModel;

/**
 * 车站信息表 John<john@cnksi.com> 2011-02-20
 */
@SuppressWarnings("all")
public class TrainsAction extends HttpServlet {

	TrainsService trainsServices = new TrainsService();
	SworksService sworksServices = new SworksService();
	RulesService rulesServices = new RulesService();

	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String cmd = request.getParameter("cmd");

		if (cmd.equals("list")) {//分页列出信息
			request.setAttribute("pm", listTrain(request));
			request.setAttribute("content_tittle", "车站列表");
			request.getRequestDispatcher("web/trains/list.jsp").forward(
					request, response);
		} else if (cmd.equals("list_sh")) {//分页列出信息
			request.setAttribute("pm", listTrain(request));
			request.setAttribute("content_tittle", "车站列表");
			request.getRequestDispatcher("web/trains/list_sh.jsp").forward(request, response);
		} else if (cmd.equals("list_dz")) {//分页列出信息
			request.setAttribute("pm", listTrain(request));
			request.setAttribute("content_tittle", "车站状态查看");
			request.getRequestDispatcher("web/trains/list_dz.jsp").forward(request, response);
		}else if (cmd.equals("adding")) {//添加前准备数据信息
			request.setAttribute("content_tittle", "添加车站");
			request.getRequestDispatcher("web/trains/input.jsp").forward(request, response);
		}else if(cmd.equals("update")){//修改信息
			int tid = Integer.parseInt(request.getParameter("tid"));
			request.setAttribute("train", findTrain(tid));
			request.setAttribute("content_tittle", "修改车站信息");
			request.getRequestDispatcher("web/trains/input.jsp").forward(request, response);
		}else if(cmd.equals("update_sh")){//修改信息
			int tid = Integer.parseInt(request.getParameter("tid"));
			request.setAttribute("train", findTrain(tid));
			request.setAttribute("content_tittle", "修改车站信息");
			request.getRequestDispatcher("web/trains/input_sh.jsp").forward(request, response);	
		} else if (cmd.equals("input")) {//添加或者修改
			if (request.getParameter("tid") == null||request.getParameter("tid").equals("")) {
				if (addTrain(request)) {
					request.setAttribute("msg", "恭喜你，你已经成功添加了一条车站的记录！");
				} else {
					request.setAttribute("msg", "对不起，你添加记录失败！");
				}
			} else {
				String tstate = request.getParameter("tstate");
				if (tstate.equals("2")&& (!addFinish(request)) ) {
					request.setAttribute("msg", "对不起，有一些规章制度审核未通过或者未审核，你不能将车站的状态改为已审核！");
				} else {
					if (updateTrain(request)) {
						request.setAttribute("msg", "恭喜你，你已经成功修改了一条车站的记录！");
					} else {
						request.setAttribute("msg", "对不起，你修改记录失败！");
					}
				}
				if (tstate.equals("3") ) {
					if (updateTrainNo(request)) {
						request.setAttribute("msg", "恭喜你，你已经成功修改了一条车站的记录！");
					} else {
						request.setAttribute("msg", "对不起，你修改记录失败！");
					}
				}
			}
			request.getRequestDispatcher("trains?cmd=list").forward(request, response);
		} else if (cmd.equals("input_sh")) {//添加或者修改
			String tstate = request.getParameter("tstate");
			if (tstate.equals("2")&& (!addFinish(request)) ) {
				request.setAttribute("msg", "对不起，有一些规章制度审核未通过或者未审核，你不能将车站的状态改为已审核！");
			} else {
				if (updateTraindzz(request)) {
					request.setAttribute("msg", "恭喜你，你已经成功修改了一条车站的记录！");
				} else {
					request.setAttribute("msg", "对不起，你修改记录失败！");
				}
			}
			
			request.getRequestDispatcher("trains?cmd=list_sh").forward(request, response);	
		} else if(cmd.equals("delete")){//删除信息
			if(deleteTrain(request)){
				request.setAttribute("msg", "恭喜你，你已经成功删除了一条车站的记录！");
			} else {
				request.setAttribute("msg", "对不起，你删除记录失败！");
			}
			request.getRequestDispatcher("trains?cmd=list").forward(request, response);
		} else if(cmd.equals("export")){
			List list = trainsServices.findTrains("SELECT * FROM Trains WHERE tstate!=10000 ORDER BY tid ASC");
			findTrainsToExcel(trainsServices.findTrainsToExcel(list),request);
			request.getRequestDispatcher("web/excel.jsp").forward(
					request, response);
		}else if (cmd.equals("submiting")) {// 提交车站准备数据
			int tid = Integer.parseInt(request.getParameter("tid"));
			List<Trains> trainslist = listTrains(request);
			request.setAttribute("trains", trainslist);
			if (tid == 0 ) {
				tid = trainslist.get(0).getTid();
			}
			request.setAttribute("number", tid);
			request.setAttribute("train", findTrain(tid));
			request.getRequestDispatcher("web/trains/submit.jsp").forward(request, response);
		}else if (cmd.equals("submit")) {// 提交车站准备数据		
			if(addFinishSh(request))
			{
				if(updateTrainsh(request)){
					request.setAttribute("msg", "恭喜你，提交成功!");
				}else{
					request.setAttribute("msg", "对不起，提交失败!");
				}
			}else{
				request.setAttribute("msg", "对不起!车站的信息没有审核完成,本次提交失败!");
			}
			int tid = Integer.parseInt(request.getParameter("tid"));
			List<Trains> trainslist = listTrains(request);
			request.setAttribute("trains", trainslist);
			request.setAttribute("number", tid);
			request.setAttribute("train", findTrain(tid));
			request.getRequestDispatcher("web/trains/submit.jsp").forward(request, response);
		}else if (cmd.equals("submitcancel")) {// 提交车站准备数据		
			if(addcancel(request))
			{
				if(updateTraincancel(request)){
					request.setAttribute("msg", "恭喜你，提交成功!");
				}else{
					request.setAttribute("msg", "对不起，提交失败!");
				}
			}else{
				request.setAttribute("msg", "对不起!段长组已经审核了一些规章制度,本次提交失败!");
			}
			int tid = Integer.parseInt(request.getParameter("tid"));
			List<Trains> trainslist = listTrains(request);
			request.setAttribute("trains", trainslist);
			request.setAttribute("number", tid);
			request.setAttribute("train", findTrain(tid));
			request.getRequestDispatcher("web/trains/submit.jsp").forward(request, response);
		}
	}

	// 分页查询火车站信息
	private PageModel listTrain(HttpServletRequest request) {
		int offset = 0;
		try {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		} catch (NumberFormatException e) {
			offset = 0;
		}
		String SQL = "SELECT * FROM Trains WHERE tstate!=10000 ORDER BY tid DESC";
		PageModel pm = trainsServices.findByPage(offset, 10, SQL);
		return pm;
	}
	
	// 车站信息
	private List<Trains> listTrains(HttpServletRequest request) {
		String SQL = "SELECT * FROM Trains WHERE tstate !=10000  ORDER BY tid ASC";
		return trainsServices.findTrains(SQL);
	}
	
	// 判断审核组是否审核完成
	private boolean addFinishSh(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		int rnums = rulesServices.findRules("SELECT * FROM Rules WHERE rlevel=4 ").size();	
		int snums = sworksServices.findSworks("SELECT * FROM Sworks WHERE ssstate= 1  AND tid = " + tid).size();
		return (rnums==snums)?true:false;
	}
	
	// 判断段长组是否审核
	private boolean addcancel(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		int snums1 = sworksServices.findSworks("SELECT * FROM Sworks WHERE sdstate= 1  AND tid = " + tid).size();
		int snums2 = sworksServices.findSworks("SELECT * FROM Sworks WHERE sdstate= 2  AND tid = " + tid).size();
		return (snums1 == 0 && snums2 == 0)?true:false;
	}
	
	// 判断车站的数据是否审核完成
	private boolean addFinish(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		int rnums = rulesServices.findRules("SELECT * FROM Rules WHERE rlevel=4 ").size();	
		int snums = sworksServices.findSworks("SELECT * FROM Sworks WHERE ssstate= 1 AND sdstate= 1 AND tid = " + tid).size();
		return (rnums==snums)?true:false;
	}
	
	// 查找火车站信息
	private Trains findTrain(int tid) {
		String SQL = "SELECT * FROM trains WHERE tid = " + tid;
		return trainsServices.findTrain(SQL);
	}
	
	// 添加火车站信息
	private boolean addTrain(HttpServletRequest request) {
		String tname = request.getParameter("tname");
		String tman = request.getParameter("tman");
		String tphone = request.getParameter("tphone");
		String tstate = request.getParameter("tstate");
		
		String SQL = "INSERT INTO trains(tname,tman,tphone,tstate) VALUES('"
				+ tname + "','" + tman + "','"+tphone + "','"+tstate + "')";
		return trainsServices.addTrain(SQL);
	}
	
	// 修改火车站信息
	private boolean updateTrain(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		String tname = request.getParameter("tname");
		String tman = request.getParameter("tman");
		String tphone = request.getParameter("tphone");
		String tstate = request.getParameter("tstate");
		String cankao = request.getParameter("cankao");
		String SQL = "UPDATE trains SET tname = '" + tname + "',tman = '"
				+ tman + "',tphone = '" + tphone + "',tstate = '" + tstate + "',cankao = '" + cankao + "' WHERE tid = " + tid;
		return trainsServices.updateTrain(SQL);
	}
	
	// 修改火车站为通过
	private boolean updateTrainNo(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		String tname = request.getParameter("tname");
		String tman = request.getParameter("tman");
		String tphone = request.getParameter("tphone");
		String tstate = request.getParameter("tstate");
		String cankao = request.getParameter("cankao");
		String SQL = "UPDATE trains SET shtstate=0, tname = '" + tname + "',tman = '"
				+ tman + "',tphone = '" + tphone + "',tstate = '" + tstate + "',cankao = '" + cankao + "' WHERE tid = " + tid;
		return trainsServices.updateTrain(SQL);
	}
	
	//审核火车站信息
	private boolean updateTrainsh(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		String SQL = "UPDATE trains SET shtstate = 1 WHERE tid = " + tid;
		return trainsServices.updateTrain(SQL);
	}
	
	
	// 取消火车站审核信息
	private boolean updateTraincancel(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		String SQL = "UPDATE trains SET shtstate = 0 WHERE tid = " + tid;
		return trainsServices.updateTrain(SQL);
	}
	
	//段长组审核
	private boolean updateTraindzz(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		int tstate = Integer.parseInt(request.getParameter("tstate"));
		String SQL = "UPDATE trains SET tstate = " + tstate + "  WHERE tid = " + tid;
		return trainsServices.updateTrain(SQL);
	}
	
	// 删除火车站信息
	private boolean deleteTrain(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		String SQL = "DELETE FROM trains WHERE tid = " + tid;
		return trainsServices.deleteTrain(SQL);
	}
	
    ///////////////////////////////////////////////////////////////////////////
	 //导出到Excel
	public String findTrainsToExcel(List<Trains> data,HttpServletRequest request){	
		CreateExcel ce = new CreateExcel();
		String filename = "train_data.xls";
		String path = request.getRealPath("/image");
		String file = request.getRealPath("/image/"+filename);
		ce.createExcel(path,file,data);
		return filename;
	}

}
